#include "../include/head.h"

// 初始化消息队列
int init_queue(taskQueue_t *queue)
{
    queue->queuesize = 0;
    queue->pFront = NULL;
    queue->pRear = NULL;
    return 0;
}

// 填入结点信息
taskNode_t* in_node(int netfd,int user_id,MSG_TYPE type,char *args,int args_len,int now_path_id)
{
    taskNode_t *new_node = (taskNode_t *)malloc(sizeof(taskNode_t));

    new_node->netfd = netfd;
    new_node->user_id = user_id;
    new_node->type = type;
    new_node->now_path_id = now_path_id;
    memcpy(new_node->args, args, args_len);

    new_node->pNext = NULL;
    return new_node;
}

// 尾插法
int tail_insert(taskQueue_t *q, taskNode_t *new_node)
{
    if(q->queuesize == 0)
    {
        q->pFront = new_node;
        q->pRear = new_node;
        q->pFront->pNext = NULL;
    }
    else
    {
        q->pRear->pNext = new_node;
        q->pRear = new_node;
        new_node->pNext = NULL;
    }
    q->queuesize++;
    return 1;
}

// 头删法
taskNode_t *head_delete(taskQueue_t *q)
{
    taskNode_t *old_node;
    old_node = q->pFront;
    if (q->queuesize == 0)
    {
        printf("没有客户可接收！\n");
        return NULL;
    }
    if(q->queuesize == 1)
    {
        q->pFront = NULL;
        q->pRear = NULL;
        q->queuesize--;
        return old_node;
    }
    q->pFront = q->pFront->pNext;
    q->queuesize--;
    return old_node;
}

// 队列打印函数
void print_node(taskNode_t *go)
{
 
    printf("netfd = %d\n", go->netfd);
    printf("user_id = %d\n", go->user_id);
    printf("type = %d\n", go->type);
    printf("now_path_id = %d\n", go->now_path_id);
    printf("args = %s\n", go->args);

    printf("----------------");

    printf("\n");
}